﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Batman.Cms.Core.Utils;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace Bamtman.ImageCenter.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ImageController : ControllerBase
    {
        private readonly IHostingEnvironment _environment;
        public ImageController(IHostingEnvironment environment)
        {
            _environment = environment;
        }

        [HttpPost]
        public ActionResult<string> Post()
        {
            var files = Request.Form.Files;
            foreach (var file in files)
            {
                var fileInfo = new FileInfo(file.FileName);
                var imagePath = ImageUtil.SaveImage(file.OpenReadStream(), fileInfo.Extension, _environment.ContentRootPath, out var success);
                if (!success) return "上传失败";
            }
            return "ok";
        }
    }
}
